using DesktopWidgetApp.UI.Helpers;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Forms;
using System.Drawing;
using Color = System.Windows.Media.Color;
using Brushes = System.Windows.Media.Brushes;

namespace DesktopWidgetApp.UI.Views
{
    /// <summary>
    /// SettingsWindow.xaml 的交互逻辑
    /// </summary>
    public partial class SettingsWindow : Window
    {
        private readonly StyleManager _styleManager;
        private StyleSettings _originalSettings;
        private StyleSettings _currentSettings;

        /// <summary>
        /// 构造函数
        /// </summary>
        public SettingsWindow()
        {
            InitializeComponent();
            
            // 初始化样式管理器
            _styleManager = new StyleManager();
            
            // 保存原始设置（深拷贝）
            _originalSettings = CloneSettings(_styleManager.CurrentSettings);
            _currentSettings = CloneSettings(_styleManager.CurrentSettings);
            
            // 加载设置到UI控件
            LoadSettingsToUI();
            
            // 设置滑块事件
            SetupSliderEvents();
        }

        /// <summary>
        /// 加载设置到UI控件
        /// </summary>
        private void LoadSettingsToUI()
        {
            if (_currentSettings == null)
                return;

            // 颜色设置
            BackgroundColorTextBox.Text = _currentSettings.BackgroundColor;
            UpdateColorPreview(BackgroundColorPreview, _currentSettings.BackgroundColor);

            BorderColorTextBox.Text = _currentSettings.BorderColor;
            UpdateColorPreview(BorderColorPreview, _currentSettings.BorderColor);

            TitleBarColorTextBox.Text = _currentSettings.TitleBarColor;
            UpdateColorPreview(TitleBarColorPreview, _currentSettings.TitleBarColor);

            TitleTextColorTextBox.Text = _currentSettings.TitleTextColor;
            UpdateColorPreview(TitleTextColorPreview, _currentSettings.TitleTextColor);

            ContentBackgroundColorTextBox.Text = _currentSettings.ContentBackgroundColor;
            UpdateColorPreview(ContentBackgroundColorPreview, _currentSettings.ContentBackgroundColor);

            ContentTextColorTextBox.Text = _currentSettings.ContentTextColor;
            UpdateColorPreview(ContentTextColorPreview, _currentSettings.ContentTextColor);

            ButtonBackgroundColorTextBox.Text = _currentSettings.ButtonBackgroundColor;
            UpdateColorPreview(ButtonBackgroundColorPreview, _currentSettings.ButtonBackgroundColor);

            ButtonTextColorTextBox.Text = _currentSettings.ButtonTextColor;
            UpdateColorPreview(ButtonTextColorPreview, _currentSettings.ButtonTextColor);

            // 滑块设置
            CornerRadiusSlider.Value = _currentSettings.CornerRadius;
            CornerRadiusText.Text = _currentSettings.CornerRadius.ToString();

            FontSizeSlider.Value = _currentSettings.FontSize;
            FontSizeText.Text = _currentSettings.FontSize.ToString();

            // 复选框设置
            ShadowEnabledCheckBox.IsChecked = _currentSettings.ShadowEnabled;
        }

        /// <summary>
        /// 更新颜色预览
        /// </summary>
        private void UpdateColorPreview(Border previewBorder, string colorString)
        {
            try
            {
                Color color = (Color)ColorConverter.ConvertFromString(colorString);
                previewBorder.Background = new SolidColorBrush(color);
            }
            catch { }
        }

        /// <summary>
        /// 设置滑块事件
        /// </summary>
        private void SetupSliderEvents()
        {
            CornerRadiusSlider.ValueChanged += (sender, e) =>
            {
                CornerRadiusText.Text = ((Slider)sender).Value.ToString();
            };

            FontSizeSlider.ValueChanged += (sender, e) =>
            {
                FontSizeText.Text = ((Slider)sender).Value.ToString();
            };
        }

        /// <summary>
        /// 从UI控件中收集设置
        /// </summary>
        private void CollectSettingsFromUI()
        {
            if (_currentSettings == null)
                _currentSettings = new StyleSettings();

            // 收集颜色设置
            _currentSettings.BackgroundColor = BackgroundColorTextBox.Text;
            _currentSettings.BorderColor = BorderColorTextBox.Text;
            _currentSettings.TitleBarColor = TitleBarColorTextBox.Text;
            _currentSettings.TitleTextColor = TitleTextColorTextBox.Text;
            _currentSettings.ContentBackgroundColor = ContentBackgroundColorTextBox.Text;
            _currentSettings.ContentTextColor = ContentTextColorTextBox.Text;
            _currentSettings.ButtonBackgroundColor = ButtonBackgroundColorTextBox.Text;
            _currentSettings.ButtonTextColor = ButtonTextColorTextBox.Text;

            // 收集滑块设置
            _currentSettings.CornerRadius = CornerRadiusSlider.Value;
            _currentSettings.FontSize = FontSizeSlider.Value;

            // 收集复选框设置
            _currentSettings.ShadowEnabled = ShadowEnabledCheckBox.IsChecked ?? false;
        }

        /// <summary>
        /// 应用当前设置
        /// </summary>
        private void ApplyCurrentSettings()
        {
            CollectSettingsFromUI();
            _styleManager.UpdateStyleSettings(_currentSettings);

            // 如果主窗口已打开，应用样式到主窗口
            if (Application.Current.MainWindow != null && Application.Current.MainWindow is MainWindow)
            {
                _styleManager.ApplyStyleToWindow(Application.Current.MainWindow, _currentSettings);
            }
        }

        /// <summary>
        /// 克隆设置对象
        /// </summary>
        private StyleSettings CloneSettings(StyleSettings source)
        {
            if (source == null)
                return null;

            return new StyleSettings
            {
                BackgroundColor = source.BackgroundColor,
                BorderColor = source.BorderColor,
                TitleBarColor = source.TitleBarColor,
                TitleTextColor = source.TitleTextColor,
                ContentBackgroundColor = source.ContentBackgroundColor,
                ContentTextColor = source.ContentTextColor,
                ButtonBackgroundColor = source.ButtonBackgroundColor,
                ButtonTextColor = source.ButtonTextColor,
                CornerRadius = source.CornerRadius,
                ShadowEnabled = source.ShadowEnabled,
                FontSize = source.FontSize,
                FontFamily = source.FontFamily
            };
        }

        /// <summary>
        /// 打开颜色选择对话框
        /// </summary>
        private string ShowColorDialog(string initialColor)
        {
            try
            {
                using (var colorDialog = new ColorDialog())
                {
                    // 设置初始颜色
                    if (!string.IsNullOrEmpty(initialColor))
                    {
                        Color wpfColor = (Color)ColorConverter.ConvertFromString(initialColor);
                        colorDialog.Color = System.Drawing.Color.FromArgb(wpfColor.R, wpfColor.G, wpfColor.B);
                    }

                    if (colorDialog.ShowDialog() == DialogResult.OK)
                    {
                        // 转换为十六进制颜色值
                        return string.Format("#{0:X2}{1:X2}{2:X2}", 
                            colorDialog.Color.R, 
                            colorDialog.Color.G, 
                            colorDialog.Color.B);
                    }
                }
            }
            catch { }

            return initialColor;
        }

        // 颜色选择按钮点击事件
        private void BackgroundColorPickerButton_Click(object sender, RoutedEventArgs e)
        {
            string color = ShowColorDialog(BackgroundColorTextBox.Text);
            if (!string.IsNullOrEmpty(color))
            {
                BackgroundColorTextBox.Text = color;
                UpdateColorPreview(BackgroundColorPreview, color);
            }
        }

        private void BorderColorPickerButton_Click(object sender, RoutedEventArgs e)
        {
            string color = ShowColorDialog(BorderColorTextBox.Text);
            if (!string.IsNullOrEmpty(color))
            {
                BorderColorTextBox.Text = color;
                UpdateColorPreview(BorderColorPreview, color);
            }
        }

        private void TitleBarColorPickerButton_Click(object sender, RoutedEventArgs e)
        {
            string color = ShowColorDialog(TitleBarColorTextBox.Text);
            if (!string.IsNullOrEmpty(color))
            {
                TitleBarColorTextBox.Text = color;
                UpdateColorPreview(TitleBarColorPreview, color);
            }
        }

        private void TitleTextColorPickerButton_Click(object sender, RoutedEventArgs e)
        {
            string color = ShowColorDialog(TitleTextColorTextBox.Text);
            if (!string.IsNullOrEmpty(color))
            {
                TitleTextColorTextBox.Text = color;
                UpdateColorPreview(TitleTextColorPreview, color);
            }
        }

        private void ContentBackgroundColorPickerButton_Click(object sender, RoutedEventArgs e)
        {
            string color = ShowColorDialog(ContentBackgroundColorTextBox.Text);
            if (!string.IsNullOrEmpty(color))
            {
                ContentBackgroundColorTextBox.Text = color;
                UpdateColorPreview(ContentBackgroundColorPreview, color);
            }
        }

        private void ContentTextColorPickerButton_Click(object sender, RoutedEventArgs e)
        {
            string color = ShowColorDialog(ContentTextColorTextBox.Text);
            if (!string.IsNullOrEmpty(color))
            {
                ContentTextColorTextBox.Text = color;
                UpdateColorPreview(ContentTextColorPreview, color);
            }
        }

        private void ButtonBackgroundColorPickerButton_Click(object sender, RoutedEventArgs e)
        {
            string color = ShowColorDialog(ButtonBackgroundColorTextBox.Text);
            if (!string.IsNullOrEmpty(color))
            {
                ButtonBackgroundColorTextBox.Text = color;
                UpdateColorPreview(ButtonBackgroundColorPreview, color);
            }
        }

        private void ButtonTextColorPickerButton_Click(object sender, RoutedEventArgs e)
        {
            string color = ShowColorDialog(ButtonTextColorTextBox.Text);
            if (!string.IsNullOrEmpty(color))
            {
                ButtonTextColorTextBox.Text = color;
                UpdateColorPreview(ButtonTextColorPreview, color);
            }
        }

        // 按钮点击事件
        private void ResetButton_Click(object sender, RoutedEventArgs e)
        {
            _currentSettings = _styleManager.GetDefaultSettings();
            LoadSettingsToUI();
        }

        private void ApplyButton_Click(object sender, RoutedEventArgs e)
        {
            ApplyCurrentSettings();
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            ApplyCurrentSettings();
            DialogResult = true;
            Close();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }
    }
}